let tb = document.querySelectorAll('table');
console.log(tb);
if (tb.length > 1) {
    let symbol = document.querySelector("body > div > table:nth-child(5) > tbody > tr:nth-child(1) > td:nth-child(2)")
        ?.textContent?.split(/[\(\s]/)?.[0];

    if (!symbol) {
        let msg = 'ERROR: no symbol found';
        console.error(msg);
        document.title = msg;
        throw new Error(msg);
    }

    let wb = XLSX.utils.table_to_book(tb[1], {sheet: "mt4optimizereport", cellDates: true});

    let ws = wb.Sheets['mt4optimizereport'];
    const range = XLSX.utils.decode_range(ws['!ref']);

    debugger
    let data = XLSX.utils.sheet_to_json(ws,
        {
            header: ['id', 'time', 'type', 'order_id',
                'lots', 'price', 'sl', 'tp', 'profit', 'balance']
        });

    let ext_col = 1;
    //XLSX.utils.sheet_add_aoa(ws, [['Profit/Drawdown$']], {origin: {r: 0, c: range.e.c + ext_col}});
    //let profit_on_drawdown = [];

    /*
email=312897211@qq.com&pass=Abc123&id=100000373&isDemo=false&accCompany=CrebitePTELTDGL
13989621,1629980992,6,0.01000000,,0.00000000,0.00000000,0.00000000,1629980992,0.00000000,0.00000000,0.00000000,324927.00000000,"Deposit",0
100000373,USD,324927.00000000,324927.00000000,1629983363,false,CrebitePTELTDGL,end
1.86

    * */

    const ids = [12959700,
        12959818,
        12959825,
        12959831,
        12959842,
        12960037,
        12960159,
        12960239,
        12960257,
        12960257,
        12960276,
        12960447,
        12960502,
        12960675,
        12960710,
        12960872,
        12960994,
        12961101,
        12961181,
        12961380,
        12961517,
        12961663,
        12961692,
        12961740,
        12961916,
        12962082,
        12962184,
        12962226,
        12962325,
        12962363,
        12962392,
        12962438,
        12962474,
        12962654,
        12962693,
        12962860,
        12962965,
        12963101,
        12963168,
        12963208,
        12963399,
        12963439,
        12963574,
        12963643,
        12963813,
        12963991,
        12964038,
        12964144,
        12964158,
        12964241,
        12964328,
        12964493,
        12964631,
        12964727,
        12964825,
        12964885,
        12964906,
        12965017,
        12965195,
        12965227,
        12965334,
        12965489,
        12965546,
        12965601,
        12965736,
        12965832,
        12965956,
        12966001,
        12966129,
        12966155,
        12966168,
        12966222,
        12966273,
        12966316,
        12966512,
        12966673,
        12966853,
        12966919,
        12966982,
        12966993,
        12967191,
        12967342,
        12967356,
        12967457,
        12967514,
        12967669,
        12967720,
        12967737,
        12967774,
        12967825,
        12967928,
        12967940,
        12968007,
        12968142,
        12968288,
        12968378,
        12968397,
        12968573,
        12968606,
        12968632,
        12968708,
        12968792,
        12968909,
        12969103,
        12969156,
        12969299,
        12969470,
        12969582,
        12969625,
        12969647,
        12969780,
        12969968,
        12970115,
        12970288,
        12970323,
        12970358,
        12970513,
        12970701,
        12970713,
        12970874,
        12970886,
        12971008,
        12971184,
        12971360,
        12971485,
        12971575,
        12971681,
        12971774,
        12971790,
        12971920,
        12971980,
        12971997,
        12972058,
        12972082,
        12972094,
        12972203,
        12972221,
        12972414,
        12972573,
        12972748,
        12972938,
        12973035,
        12973126,
        12973256,
        12973406,
        12973590,
        12973648,
        12973816,
        12973944,
        12974122,
        12974146,
        12974275,
        12974356,
        12974537,
        12974721,
        12974894,
        12974999,
        12975030,
        12975138,
        12975304,
        12975385,
        12975578,
        12975631,
        12975742,
        12975882,
        12975946,
        12976121,
        12976241,
        12976269,
        12976281,
        12976339,
        12976441,
        12976467,
        12976522,
        12976532,
        12976586,
        12976605,
        12976787,
        12976880,
        12976988,
        12977053,
        12977098,
        12977256,
        12977369,
        12977439,
        12977525,
        12977534,
        12977698,
        12977708,
        12977814,
        12977828,
        12977849,
        12977922,
        12978085,
        12978163,
        12978171,
        12978173,
        12978337,
        12978438,
        12978450,
        12978524,
        12978525,
        12978560,
        12978625,
        12978661,
        12978768,
        12978778,
        12978857,
        12979047,
        12979112,
        12979161,
        12979322,
        12979443,
        12979511,
        12979625,
        12979769,
        12979812,
        12979883,
        12980003,
        12980111,
        12980270,
        12980300,
        12980434,
        12980452,
        12980522,
        12980686,
        12980855,
        12980942,
        12981126,
        12981237,
        12981253,
        12981306,
        12981343,
        12981440,
        12981534,
        12981678,
        12981873,
        12981913,
        12982045,
        12982083,
        12982260,
        12982348,
        12982485,
        12982563,
        12982619,
        12982653,
        12982701,
        12982832,
        12982901,
        12982932,
        12983093,
        12983206,
        12983359,
        12983382,
        12983433,
        12983539,
        12983723,
        12983918,
        12984090,
        12984203,
        12984377,
        12984422,
        12984589,
        12984645,
        12984809,
        12984994,
        12985039,
        12985163,
        12985248,
        12985438,
        12985462,
        12985600,
        12985606,
        12985656,
        12985668,
        12985802,
        12985823,
        12985995,
        12986021,
        12986133,
        12986299,
        12986316,
        12986324,
        12986404,
        12986568,
        12986623,
        12986712,
        12986716,
        12986784,
        12986852,
        12986983,
        12987120,
        12987131,
        12987176,
        12987182,
        12987341,
        12987352,
        12987421,
        12987425,
        12987537,
        12987665,
        12987677,
        12987722,
        12987831,
        12988028,
        12988159,
        12988171,
        12988176,
        12988192,
        12988235,
        12988349,
        12988480,
        12988609,
        12988797,
        12988996,
        12989040,
        12989162,
        12989223,
        12989418,
        12989497,
        12989551,
        12989643,
        12989821,
        12989907,
        12989908,
        12990014,
        12990066,
        12990112,
        12990177,
        12990258,
        12990327,
        12990440,
        12990560,
        12990644,
        12990710,
        12990841,
        12990889,
        12990909,
        12991014,
        12991155,
        12991205,
        12991207,
        12991335,
        12991478,
        12991669,
        12991745,
        12991893,
        12992053,
        12992173,
        12992187,
        12992355,
        12992454,
        12992545,
        12992634,
        12992701,
        12992831,
        12993020,
        12993197,
        12993302,
        12993334,
        12993449,
        12993646,
        12993675,
        12993785,
        12993901,
        12994091,
        12994091,
        12994222,
        12994420,
        12994495,
        12994686,
        12994690,
        12994695,
        12994715,
        12994848,
        12994978,
        12995025,
        12995170,
        12995328,
        12995445,
        12995546,
        12995620,
        12995625,
        12995647,
        12995802,
        12995940,
        12995973,
        12996064,
        12996105,
        12996128,
        12996297,
        12996313,
        12996463,
        12996599,
        12996768,
        12996859,
        12996981,
        12997016,
        12997158,
        12997197,
        12997353,
        12997487,
        12997498,
        12997611,
        12997662,
        12997664,
        12997680,
        12997847,
        12997889,
        12997990,
        12998036,
        12998158,
        12998254,
        12998256,
        12998372,
        12998440,
        12998477,
        12998516,
        12998649,
        12998728,
        12998797,
        12998955,
        12999062,
        12999192,
        12999292,
        12999337,
        12999412,
        12999549,
        12999587,
        12999598,
        12999659,
        12999749,
        12999787,
        12999945,
        13000046,
        13000199,
        13000343,
        13000462,
        13000486,
        13000528,
        13000679,
        13000715,
        13000794,
        13000864,
        13000910,
        13000923,
        13000994,
        13001181,
        13001256,
        13001384,
        13001434,
        13001541,
        13001644,
        13001676,
        13001696,
        13001700,
        13001883,
        13001958,
        13002109,
        13002167,
        13002284,
        13002459,
        13002481,
        13002543,
        13002710,
        13002781,
        13002850,
        13002902,
        13002961,
        13003155,
        13003196];

    const get_random_id = (base) => {
        return Math.round(Math.random() * 200) + base;
    };

    const get_op_type = (op) => {
        switch (op) {
            case 'buy':
                return 0;
            case 'sell':
                return 1;
            case 'balance':
                return 6;
        }

        return 0;
    }

    const get_order_by_id = (id, row) => {
        for (let r = row - 1; r > range.s.r; r--) {
            let d = data[r];
            if (d.order_id === id) return d;
        }

        return null;
    };

    let buffer = '';

    let last_balance = 0.0;
    let total_swap = 0.0;
    let total_lots = 0.0;
    let id = 70674301;

    const swap_ratio = 3;

    let id_index = 0;

    // id, open_time, op_type, lots, symbol, open_price, sl, tp, close_time, close_price, commission, swap, profit, comment, magic_number
    for (let r = range.s.r + 1; r < range.e.r; r++) {
        let d = data[r];
        if (d.type === 'close') {
            id = get_random_id(id);

            let line = id + ',';

            let order_start = get_order_by_id(d.order_id, r);
            if (!order_start) {
                console.error(`can't found started of order ${d.order_id}`);
                continue;
            }

            id_index++;

            let swap = (os, oe) => {
                let dt = oe.time - os.time;
                if (dt >= 3 * 24 * 3600) {
                    if (os.type === 'buy') {
                        return -1 * Number(Math.abs(oe.profit) * 0.0215).toFixed(2);
                    } else if (os.type === 'sell') {
                        return -1 * Number(Math.abs(oe.profit) * 0.0035).toFixed(2);
                    }
                }
                return 0;
            };

            line += `${Math.round((new Date(order_start.time)).getTime() / 1000)},`;
            line += `${get_op_type(order_start.type)},`;
            line += `${Number(order_start.lots).toFixed(8)},`;
            line += `${symbol},`;
            line += `${Number(order_start.price).toFixed(8)},`;
            line += `${Number(order_start.sl).toFixed(8)},`;
            line += `${Number(order_start.tp).toFixed(8)},`;
            line += `${Math.round((new Date(d.time)).getTime() / 1000)},`;
            line += `${Number(d.price).toFixed(8)},`;
            line += `${Number(0).toFixed(8)},`;     // commission
            line += `${(-1 * Number(order_start.lots * swap_ratio)).toFixed(8)},`;     // swap
            line += `${Number(d.profit).toFixed(8)},`;
            line += `"",`;	// comment
            line += `0`;	// magic number
            // console.log(line);

            last_balance = Number(d.balance);
            total_lots += Number(order_start.lots);

            if (buffer) buffer += "\r\n";
            buffer += line;
        }
    }

    //XLSX.utils.sheet_add_aoa(ws, profit_on_drawdown, {origin: {r: 1, c: range.e.c + ext_col}});

    let fname = (document.title ?? 'out');
    XLSX.writeFile(wb, fname + '.xlsb');
    console.log(buffer);
    console.log("last blance: ", last_balance);
    console.log("total swap: ", total_lots * swap_ratio);
    console.log("total lots: ", total_lots);

}